package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CollectParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.model.MarketCollect;
import com.cskaoyan.model.MarketCollectExample;
import com.cskaoyan.service.CollectService;
import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class CollectServiceImpl implements CollectService {
    @Override
    public PageData list(CollectParameter collectParameter) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketCollectMapper mapper = sqlsession.getMapper(MarketCollectMapper.class);
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        //分页
        Integer page = collectParameter.getPage();
        Integer limit = collectParameter.getLimit();
        PageHelper.startPage(page, limit);
        //排序
        String sort = collectParameter.getSort();
        String order = collectParameter.getOrder();
        marketCollectExample.setOrderByClause(sort + " " + order);
        //条件
        Date current = new Date();
        Integer userId = collectParameter.getUserId();
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }
        Integer valueId = collectParameter.getValueId();
        if (valueId != null && valueId != 0) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> collects = mapper.selectByExample(marketCollectExample);
        for (MarketCollect collect : collects) {
            collect.setAddTime(current);
            collect.setUpdateTime(current);
        }

        sqlsession.commit();
        sqlsession.close();
        return PageData.list(collects);
    }
}
